<?php

/**
 * Name: 签到记录模型层
 * User: Silent
 * Date: 2025-11-07
 * Time: 15:00:00
 */

namespace App\Models\Checkin;

use App\Models\Config\Employee;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SignInRecord extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'sign_in_records';

    protected $guarded = [];

    protected $fillable = [
        'employee_id', 'employee_name', 'team_name', 'company', 'department',
        'sign_date', 'sign_time', 'has_injury', 'is_tired', 'after_drinking',
        'is_sleepy', 'over_played', 'absent_over_10_days', 'is_new_or_transferred',
        'has_urgent_family', 'had_trouble_on_way', 'has_social_dispute',
        'drink_alcohol', 'blood_pressure_systolic', 'blood_pressure_diastolic',
        'temperature', 'remark', 'status', 'abnormal_desc'
    ];

    protected $casts = [
        'sign_date' => 'date:Y-m-d',
        'sign_time' => 'datetime:Y-m-d H:i:s',
        'has_injury' => 'boolean',
        'is_tired' => 'boolean',
        'after_drinking' => 'boolean',
        'is_sleepy' => 'boolean',
        'over_played' => 'boolean',
        'absent_over_10_days' => 'boolean',
        'is_new_or_transferred' => 'boolean',
        'has_urgent_family' => 'boolean',
        'had_trouble_on_way' => 'boolean',
        'has_social_dispute' => 'boolean',
        'drink_alcohol' => 'boolean',
        'temperature' => 'decimal:1',
        'abnormal_desc' => 'json',
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
    ];

    protected static $logName = 'sign_in_records';

    // 状态常量
    const STATUS_NORMAL = 'normal';
    const STATUS_ABNORMAL = 'abnormal';

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 关联员工
     */
    public function employee()
    {
        return $this->belongsTo(Employee::class, 'employee_id');
    }

    /**
     * 获取状态文本
     */
    public function getStatusTextAttribute()
    {
        return $this->status === self::STATUS_NORMAL ? '正常' : '异常';
    }

    /**
     * 检查是否为异常签到（严格按照医学标准）
     */
    public function isAbnormal()
    {
        // 检查十项基本内容是否有任何一项为"是"
        $dangerChecks = [
            'has_injury', 'is_tired', 'after_drinking', 'is_sleepy', 'over_played',
            'absent_over_10_days', 'is_new_or_transferred', 'has_urgent_family',
            'had_trouble_on_way', 'has_social_dispute'
        ];

        foreach ($dangerChecks as $check) {
            if ($this->{$check}) {
                return true;
            }
        }

        // 检查是否饮酒
        if ($this->drink_alcohol) {
            return true;
        }

        // 检查血压是否异常（医学标准）
        // 正常血压范围：收缩压90-139mmHg，舒张压60-89mmHg
        // 参考：中国高血压防治指南、WHO标准
        if ($this->blood_pressure_systolic < 90 || $this->blood_pressure_systolic > 139) {
            return true;
        }
        if ($this->blood_pressure_diastolic < 60 || $this->blood_pressure_diastolic > 89) {
            return true;
        }

        // 检查体温是否异常（医学标准）
        // 正常体温范围：36.0-37.3℃（口腔/腋下温度）
        // 参考：临床医学体温测量标准
        if ($this->temperature < 36.0 || $this->temperature > 37.3) {
            return true;
        }

        return false;
    }

    /**
     * 收集所有异常项目信息
     */
    public function collectAbnormalItems()
    {
        $abnormalItems = [];

        // 班前危险排查项目
        $dangerChecks = [
            'has_injury' => '有伤病',
            'is_tired' => '连续工作劳累',
            'after_drinking' => '酒后上班',
            'is_sleepy' => '失眠困乏',
            'over_played' => '玩乐过度',
            'absent_over_10_days' => '离岗时间超过10天',
            'is_new_or_transferred' => '新到岗或调岗',
            'has_urgent_family' => '家庭有紧急事务',
            'had_trouble_on_way' => '到岗途中遇到麻烦',
            'has_social_dispute' => '有严重社会纠葛'
        ];

        foreach ($dangerChecks as $field => $label) {
            if ($this->{$field}) {
                $abnormalItems[] = [
                    'type' => 'danger_check',
                    'field' => $field,
                    'label' => $label,
                    'value' => true
                ];
            }
        }

        // 饮酒检查
        if ($this->drink_alcohol) {
            $abnormalItems[] = [
                'type' => 'alcohol',
                'field' => 'drink_alcohol',
                'label' => '饮酒',
                'value' => true
            ];
        }

        // 血压异常
        if ($this->blood_pressure_systolic < 90 || $this->blood_pressure_systolic > 139) {
            $status = $this->blood_pressure_systolic < 90 ? '偏低' : '偏高';
            $abnormalItems[] = [
                'type' => 'blood_pressure',
                'field' => 'blood_pressure_systolic',
                'label' => "收缩压{$status}",
                'value' => $this->blood_pressure_systolic,
                'unit' => 'mmHg',
                'normal_range' => '90-139'
            ];
        }

        if ($this->blood_pressure_diastolic < 60 || $this->blood_pressure_diastolic > 89) {
            $status = $this->blood_pressure_diastolic < 60 ? '偏低' : '偏高';
            $abnormalItems[] = [
                'type' => 'blood_pressure',
                'field' => 'blood_pressure_diastolic',
                'label' => "舒张压{$status}",
                'value' => $this->blood_pressure_diastolic,
                'unit' => 'mmHg',
                'normal_range' => '60-89'
            ];
        }

        // 体温异常
        if ($this->temperature < 36.0 || $this->temperature > 37.3) {
            $status = $this->temperature < 36.0 ? '偏低' : '偏高';
            $abnormalItems[] = [
                'type' => 'temperature',
                'field' => 'temperature',
                'label' => "体温{$status}",
                'value' => $this->temperature,
                'unit' => '℃',
                'normal_range' => '36.0-37.3'
            ];
        }

        return $abnormalItems;
    }
}

